package org.steve.betterattack;

import org.bukkit.Bukkit;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.*;

public class GUIListener implements Listener {
    @EventHandler
    public void onInventoryClick(InventoryClickEvent event) throws Exception {
        if (event.isCancelled() || !(event.getWhoClicked() instanceof Player)) {
            return;
        }
        if (!GUI.PageNames.contains(event.getView().getTitle())){
            return;
        }
        String title=event.getView().getTitle();
        Player player=(Player) event.getWhoClicked();
        if(title==GUI.MainPageTitle){
            event.setCancelled(true);
            switch (event.getRawSlot()){
                case 9:
                    player.closeInventory();
                    player.openInventory(GUI.getInventoryCopy(GUI.CraftingPage));
                    break;
                case 0:
                    player.closeInventory();
                    player.openBook(GUI.HelpPage);
                    player.getInventory().addItem(GUI.HelpPage);

                    break;
                case 1:
                    GUI.plugin.getCommand("ba").execute(player,"ba",new String[]{"about"});
                    break;
                default:
                    break;
            }
        }
        else if(title==GUI.CraftingPageTitle){
            int click=event.getRawSlot();
            CraftingRecipe recipe=null;
            ItemStack[] itemStacks=event.getView().getTopInventory().getContents();
            for (Map.Entry<String,CraftingRecipe> entry:GUI.plugin.RegisteredCraftingRecipe.entrySet()){
                if(entry.getValue().isThis(new ItemStack[]{itemStacks[0],itemStacks[1],itemStacks[2],itemStacks[9],itemStacks[10],itemStacks[11],itemStacks[18],itemStacks[19],itemStacks[20]}))
                {
                    if (recipe==null){
                        recipe= entry.getValue();
                        event.getView().getInventory(0).setItem(16,recipe.getItemStack());
                        updateInventoryLater(player,1);
                        if (GUI.plugin.debug) GUI.plugin.getLogger().info(recipe.id);
                        if(!GUI.plugin.debug)
                            break;
                    }
                    else{
                        if(GUI.plugin.debug)
                            throw new Exception("配方冲突:"+entry.getValue().id+"和"+recipe.id);
                    }
                }
            }
            if(recipe==null && event.getView().getTopInventory().getItem(16)!=null){
                event.getView().getTopInventory().setItem(16,null);
                updateInventoryLater(player);
            }
            if(click>2&&click<9)event.setCancelled(true);
            if(click>11&&click<18)event.setCancelled(true);
            if(click>20&&click<27)event.setCancelled(true);
            if(click==17){
                if (recipe!=null) {
                    for (int i:new int[] {0,1,2,9,10,11,18,19,20}){
                        ItemStack itemStack= event.getClickedInventory().getItem(i);
                        if(itemStack==null)continue;
                        if(itemStack.getAmount()!=1){
                            itemStack.setAmount(itemStack.getAmount()-1);
                            event.getClickedInventory().setItem(i,itemStack);
                        }
                        else event.getClickedInventory().setItem(i,null);
                    }
                    player.getInventory().addItem(recipe.getItemStack());
                }
            }
            if(event.isCancelled()){
                player.updateInventory();
            }
        }
    }
    @EventHandler
    public void onCloseInventory(InventoryCloseEvent event){
        String title=event.getView().getTitle();
        if(title==GUI.CraftingPageTitle){
            Player player= (Player) event.getPlayer();
            List<ItemStack> returnBack=new ArrayList<>();
            for (int i:new int[] {0,1,2,9,10,11,18,19,20}) {
                ItemStack itemStack = event.getInventory().getItem(i);
                if (itemStack != null) {
                    returnBack.add(itemStack);
                    event.getInventory().setItem(i,null);
                }
            }
            for(ItemStack item:returnBack){
                if(player.getInventory().addItem(item).keySet().stream().count()!=0){
                    player.getLocation().getWorld().dropItem(player.getLocation(),item);
                }
            }
        }
    }
    private void updateInventoryLater(Player player,int tick){
        new BukkitRunnable() {
            @Override
            public void run() {
                player.updateInventory();
            }
        }.runTaskLater(GUI.plugin,tick);
    }
    private void updateInventoryLater(Player player){
        updateInventoryLater(player,1);
    }
}
